package com.yanwq.sayhi;

import android.app.Application;
import android.content.Context;

import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiskCache;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.utils.StorageUtils;
import com.orm.SugarApp;

import java.io.File;
import java.net.URISyntaxException;

import io.socket.client.IO;
import io.socket.client.Socket;

/**
 * 作者：yanwq。
 * 日期：2016/1/26 17:15。
 */
public class MApplication extends SugarApp {
    public static final boolean debug = false;
    private static Context context;

    private Socket mSocket;

    {
        try {
            mSocket = IO.socket("http://192.168.1.188:3000");
        } catch (URISyntaxException e) {
            throw new RuntimeException(e);
        }
    }

    public Socket getSocket() {
        return mSocket;
    }
    @Override
    public void onCreate() {
        super.onCreate();
        context = this;
        initImageLoader();
    }

    public static Context getContext() {
        return context;
    }

    public void initImageLoader() {
        // This configuration tuning is custom. You can tune every option, you may tune some of them,

        // or you can create default configuration by

        //  ImageLoaderConfiguration.createDefault(this);

        // method.

        File cacheDir = StorageUtils.getOwnCacheDirectory(getApplicationContext(), "/SayHi/cache/");
        ImageLoaderConfiguration
                config =
                new ImageLoaderConfiguration.Builder(getApplicationContext())
                        .threadPriority(Thread.NORM_PRIORITY - 1)
                        .denyCacheImageMultipleSizesInMemory()
                        .defaultDisplayImageOptions(getDefaultDisplayImageOptions())
                        .diskCache(new UnlimitedDiskCache(cacheDir))
                        .diskCacheFileNameGenerator(new Md5FileNameGenerator())
                        .diskCacheSize(500 * 1024 * 1024) // 500 Mb
                        .tasksProcessingOrder(QueueProcessingType.LIFO)
                                //.writeDebugLogs() // Remove for release app
                        .build();
        // Initialize ImageLoader with configuration.
        ImageLoader.getInstance().init(config);
    }

    public DisplayImageOptions getDefaultDisplayImageOptions() {
        return new DisplayImageOptions.Builder()
                .showImageOnLoading(R.color.white1)
                .showImageForEmptyUri(R.color.white1)
                .showImageOnFail(R.color.white1)
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .considerExifParams(true)
                .displayer(new FadeInBitmapDisplayer(500, true, true, false))
                .build();
    }
}
